package com.zsummer.zero.thread.RelativeClass;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * CountDownLatch ��ʹ��
 * @author zhouyujiang
 *
 */
public class Demo1 {

	public static void main(String[] args) {
		
		CountDownLatch startSignal = new CountDownLatch(1);
		CountDownLatch doneSignal = new CountDownLatch(10);
		
		for (int i = 0; i < 10; i++) {
			new Thread(new Driver(startSignal, doneSignal)).start();
		}
		try {
			TimeUnit.SECONDS.sleep(5);
			startSignal.countDown(); // �����߳�����ʼ
			doneSignal.await(); // �����ȴ����߳̽���
			System.out.println("���߳̽���");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class Driver implements Runnable{
	
	private CountDownLatch startSignal;
	private CountDownLatch doneSignal;
	
	public Driver(CountDownLatch startSignal, CountDownLatch doneSignal) {
		this.startSignal = startSignal;
		this.doneSignal = doneSignal;
	}

	@Override
	public void run() {
		try {
			startSignal.await();
			System.out.println(Thread.currentThread() + "----  begin");
			System.out.println(Thread.currentThread() + "----  doSomething");
			System.out.println(Thread.currentThread() + "----  end");
			doneSignal.countDown();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
	
}
